<!--个人中心页面 -->
<script setup>
import { ref, onMounted, computed, markRaw } from 'vue' // 引入 markRaw
import { useRouter } from 'vue-router'
import MyPost from '@/components/MyPost.vue'
import { Document, ChatDotSquare, StarFilled } from '@element-plus/icons-vue'
import { formatDate } from '@/utils/date'


const router = useRouter()
const activeNav = ref('我的帖子')
const currentTab = ref('Post')
const menuItem = ref([
    {
        label: '我的帖子',
        value: 'Post',
        icon: markRaw(Document), // 使用 markRaw 标记组件
    },
    {
        label: '我的评论',
        value: 'MyComment',
        icon: markRaw(ChatDotSquare) // 使用 markRaw 标记组件
    },
    {
        label: '我的收藏',
        value: 'MyCollection',
        icon: markRaw(StarFilled) // 使用 markRaw 标记组件
    },
])


const userInfo = ref({
    username: '',
    createTime: '',
    followers: 0,
    following: 0,
    avatar: ''
})


const goToHome = () => router.push('/')

// 处理置顶帖子
const handleTopPost = (postId) => {
    console.log('置顶帖子:', postId)
    // 这里可以添加置顶逻辑
}

// 处理删除帖子
const handleDeletePost = (postId) => {
    console.log('删除帖子:', postId)
    // 这里可以添加删除逻辑
}

onMounted(() => {
    const storedUser = JSON.parse(localStorage.getItem('userInfo')) || {}
    userInfo.value = {
        username: storedUser.username || '用户名',
        createTime: formatDate(storedUser.createTime || new Date()),
        followers: storedUser.followers || 100,
        following: storedUser.following || 50,
        avatar: storedUser.avatar || '@/assets/default-avatar.png'
    }
})

const componentMap = {
    Post: MyPost,
    
}
const currentComponent = computed(() => componentMap[currentTab.value])
</script>

<template>
    <div class="profile-wrapper">
        <div class="profile-container">
            <div class="back-home">
                <el-button type="primary" @click="goToHome">返回首页</el-button>
            </div>

            <div class="profile-header">
                <div class="user-info-section">
                    <div class="avatar">
                        <img :src="userInfo.avatar" alt="用户头像">
                    </div>
                    <div class="user-info">
                        <h2>{{ userInfo.username }}</h2>
                        <p>注册时间：{{ userInfo.createTime }}</p>
                        <p>粉丝：{{ userInfo.followers }} | 关注：{{ userInfo.following }}</p>
                    </div>
                </div>

                <div class="updateUser">
                    <el-button type="info" plain>编辑信息</el-button>
                </div>
            </div>

            <div class="main-content">
                <div class="left-side">
                    <h3>个人中心</h3>
                    <ul class="nav-list">
                        <li v-for="item in menuItem" :key="item" @click="currentTab = item.value, activeNav = item.label"
                            :class="{ active: activeNav === item.label }">
                            <div class="nav-item">
                                <el-icon v-if="item.icon">
                                       <component :is="item.icon" />
                                 </el-icon>
                            </div>
                            {{ item.label }}
                        </li>
                    </ul>
                </div>

                <div class="right-side">
                    <div class="right-side-header">
                        <h3>{{ activeNav }}</h3>
                    </div>

                    <!-- 内容 -->
                    <div class="content-section">
                        <component :is="currentComponent"></component>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.profile-wrapper {
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f5f7fa;
    padding: 20px;
}

.profile-container {
    max-width: 800px; // 将最大宽度从1200px改为800px
    width: 100%;
    margin: 0 auto; // 保持居中
    padding: 20px;
    position: relative;
    border-radius: 8px;
}


.back-home {
    position: absolute;
    top: 20px;
    right: 20px;
    z-index: 10;

    .el-button {
        background-color: #f3f6f8;
        border-color: #f3f6f8;
        color: #586370;
        padding: 8px 16px;
        border-radius: 20px;
        transition: all 0.3s ease;

        &:hover {
            background-color: #e9ecef;
            border-color: #e9ecef;
        }
    }
}

.profile-header,
.left-side,
.right-side,
.post-item {
    background-color: #fff;
    border-radius: 8px;
    // box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.left-side {
   max-height:500px ;
}

.profile-header {
    padding: 20px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);

    .user-info-section {
        display: flex;
        align-items: center;
        margin-bottom: 20px;

        .avatar {
            width: 80px;
            height: 80px;
            margin-right: 20px;

            img {
                width: 100%;
                height: 100%;
                border-radius: 50%;
                object-fit: cover;
            }
        }

        .user-info {
            h2 {
                margin: 0;
                font-size: 24px;
            }

            p {
                margin: 5px 0;
                color: #666;
            }
        }

        .updateUser {
            // margin-top: 20px;
        }
    }

}

.main-content {
    display: flex;
    margin-top: 30px;

    .left-side {
        width: 200px;
        margin-right: 20px;
        padding: 20px;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);

        h3 {
            margin-bottom: 20px;
            font-size: 18px;
            position: relative;
            padding-bottom: 15px; // 增加padding-bottom的值

            &::after {
                content: '';
                position: absolute;
                left: 0;
                bottom: 0;
                width: 100%;
                height: 1px;
                background-color: #e4e7ed;
            }
        }

        .nav-list {
            list-style: none;
            padding: 0;

            li {
                padding: 12px 16px;
                cursor: pointer;
                border-radius: 4px;
                display: flex;
                align-items: center;
                margin-bottom: 8px;

                &:hover {
                    background-color: #f5f5f5;
                }

                &.active {
                    background-color: #409eff;
                    color: #fff;
                    font-weight: 500;
                    box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
                }

                .el-icon {
                    margin-right: 12px;
                    font-size: 18px;
                }
            }
        }
    }

    .right-side {
        flex: 1;
        padding: 20px;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);

        .right-side-header {
            h3 {
                margin-bottom: 20px;
                font-size: 18px;
                position: relative;
                padding-bottom: 15px; // 增加padding-bottom的值

                &::after {
                    content: '';
                    position: absolute;
                    left: 0;
                    bottom: 0;
                    width: 100%;
                    height: 1px;
                    background-color: #e4e7ed;
                }
            }
        }
    }
}

.updateUser {
    .el-button {
        background-color: #f3f6f8;
        border-color: #f3f6f8;
        color: #586370;

        &:hover {
            background-color: #e9ecef;
            border-color: #e9ecef;
        }
    }
}
</style>